<template>
  <div class="content-div">
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <!-- <div class="clear-msg">
        <span @click="clearMsg">清除未读消息</span>
      </div> -->
      <div class="msg-item-box" @click="toPage('/mineMsgDetail', {type: '1', member_id: sysMsgList[0].member_id, wid: sysMsgList[0].wid, have_no_read: sysNoRead > 0 ? 1 : 0})">
        <van-badge :dot="sysNoRead > 0 ? true : false">
          <img src="@/assets/images/icon-msg.png" alt="" srcset="">
        </van-badge>
        <div class="msg-content-box">
          <div class="msg-name-box">
            <span>系统消息</span>
            <span>{{sysMsgList && sysMsgList.length ? sysMsgList[0].createTime : ''}}</span>
          </div>
          <span class="msg-content">{{sysMsgList && sysMsgList.length ? sysMsgList[0].content : '暂时没有新的消息哦~'}}</span>
        </div>
      </div>
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <div @click="toPage('/mineMsgDetail', {type: '3', member_id: item.member_id, have_no_read: item.noReadNum > 0 ? 1 : 0, wid: item.wid, userHeadSculpture: item.userHeadSculpture})" class="msg-item-box" v-for="(item, index) in list" :key="index">
          <van-badge :dot="item.noReadNum > 0 ? true : false">
            <img :src="item.userHeadSculpture" alt="" srcset="">
          </van-badge>
          <div class="msg-content-box">
            <div class="msg-name-box">
              <span>{{item.memberName}}</span>
              <span>{{item.createTime}}</span>
            </div>
            <span class="msg-content">{{item.content}}</span>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import { Toast } from 'vant'
import { selectNotificationMessageNewList, selectIsReadeList, getNotificationMessageNewSys } from '@/api/medical.js'
export default {
  name: 'index',
  data () {
    return {
      isLoading: false,
      loading: false,
      finished: false,
      list: [],
      pageParam: {
        pageSize: 20,
        pageNo: 1
      },
      sysNoRead: 0,
      sysMsgList: []
    }
  },
  created () {},
  computed: {
    accompanying_physician_id () {
      return this.$store.state.user.severUserInfo.accompanying_physician_id
    }
  },
  methods: {
    getSysMsg () { // 系统消息
      getNotificationMessageNewSys({
        type: '1',
        accompanying_physician_id: this.accompanying_physician_id,
        ...this.pageParam
      }).then(res => {
        if (res.data.errcode === 0 && res.data.data && res.data.data.id) {
          this.sysMsgList[0] = res.data.data
        }
      })
    },
    getIsMsg () {
      selectIsReadeList({
        accompanying_physician_id: this.accompanying_physician_id,
        userType: '1'
      }).then(res => {
        if (res.data.errcode === 0) {
          this.sysNoRead = res.data.data[0].isRead
        }
      })
    },
    toPage (path, query) {
      this.$router.push({
        path,
        query
      })
    },
    onLoad () {
      selectIsReadeList({
        accompanying_physician_id: this.accompanying_physician_id,
        userType: '1',
        is_accompanying_read: '1'
      }).then(res => {
        if (res.data.errcode === 0) {
          selectNotificationMessageNewList({
            accompanying_physician_id: this.accompanying_physician_id,
            // type: '3',
            ...this.pageParam
          }).then(ret => {
            if (ret.data.errcode === 0) {
              ret.data.data.results.forEach(item => {
                res.data.data.forEach(hmsg => {
                  if (hmsg.accompanying_physician_id === item.accompanying_physician_id) {
                    item.noReadNum = hmsg.isRead
                  }
                })
              })
              this.list = this.list.concat(ret.data.data.results)
              this.loading = false
              if (ret.data.data.results.length <= 0 || ret.data.data.results.length < this.pageParam.pageSize) {
                this.finished = true
              }
              this.pageParam.pageNo += 1
            }
          })
        }
      })
    },
    clearMsg () {
      console.log(1)
    },
    onRefresh () {
      setTimeout(() => {
        Toast('刷新成功')
        this.isLoading = false
      }, 1000)
    }
  },
  mounted () {
    // console.log(this.$store.state.user.severUserInfo)
    // this.getIsMsg()
    this.getSysMsg()
  }
}
</script>

<style scoped lang="less">
  .content-div{
    font-size: 12px;
    background-color: #FFFFFF;
    min-height: 100%;
    background-image: url('../../../../../assets/images/bg.png');
    background-repeat: no-repeat;
    background-size: 100% auto;
    .clear-msg{
      height: 34px;
      text-align: right;
      line-height: 34px;
      padding-right: 10px;
    }
    .msg-item-box{
      width: 300px;
      margin-left: 10px;
      border-bottom: 1px solid #F8F8F8;
      box-sizing: border-box;
      padding: 15px 10px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      position: relative;
      img{
        width: 36px;
        height: 36px;
        border-radius: 50%;
      }
      .msg-content-box{
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: flex-start;
        width: calc(100% - 46px);
        margin-left: 10px;
        .msg-name-box{
          display: flex;
          flex-direction: row;
          justify-content: space-between;
          align-items: center;
          width: 100%;
          span:last-child{
            font-size: 10px;
          }
        }
        .msg-content{
          width: 100%;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          word-break: break-all;
          padding-top: 4px;
        }
      }
    }
  }
</style>
